package commonlib 
{
	import config.DConfig;
	
	import hall.DHall;

	/**
	 * ...
	 * @author Sing.G
	 */
	public class DRoomInfo 
	{
		static private var _this:DRoomInfo = null;
		
		private var m_currRoomid:int = 0;		//当前房间ID
		private var m_duihuanDatas:Object;		//兌換數據
		private var m_roomList:Array = [];
		
		public function DRoomInfo() 
		{
			_this = this;
		}
		
		static public function getThis():DRoomInfo
		{
			if (!_this) _this = new DRoomInfo();
			return _this;
		}
		
		private function sortOnNeedgold(a:Object, b:Object):Number 
		{
			var iA:int = a.needgold;
			var iB:int = b.needgold;
			var result:int = iA > iB ? 1 : (iA < iB ? -1 : 0);
			return result;
		}
		
		public function setRoomList(list:Array):void
		{
			m_roomList = list;
		}
		
		public function getRoomGameName(roomId:int):String 
		{
			var item:Object = getRoomInfoById(roomId);
			return item.gameNameCN;
		}
		
		public function getRoomInfoById(roomId:int):Object {
			var len:int = m_roomList.length;
			var item:Object = { };
			for (var i:int = 0; i < len; i++)
			{
				item = m_roomList[i];
				if (item.id == roomId) break;
			}
			item.gameName = DConfig.ARY_GAME_NAME[item.gameid];
			item.gameNameCN = DConfig.ARY_GAME_NAME_CN[item.gameid];
			return item;
		}
		
		/**
		 * 獲取房間信息
		 * @param	gameid			遊戲ID
		 * @param	index 1 - 4		遊戲的4個房間index
		 * @return	{id:"", name:"", peilv:"", needlevel:"", needgold:"", numPeople:"", maxPeople:"", gameid:"", gameName:"", gameNameCN:""}
		 */
		public function getRoomInfo(gameid:int, index:int):Object
		{
			var len:int = m_roomList.length;
			var item:Object = { };
			var tmpList:Array = [];
			for (var i:int = 0; i < len; i++)
			{
				item = m_roomList[i];
				if (item.gameid == gameid) tmpList.push(item);
			}
			tmpList.sort(sortOnNeedgold);
			var info:Object = tmpList[index - 1] ? tmpList[index - 1] : null;
			if (info)
			{
				info.gameName = DConfig.ARY_GAME_NAME[item.id];
				info.gameNameCN = DConfig.ARY_GAME_NAME_CN[item.id];
			}
			return info;
		}

		/**
		 * 更新房間信息
		 * @param	roomid
		 * @param	data
		 */
		public function setRoomInfo(roomid:int, data:Object):void
		{
			var i:int = 0;
			for each(var item:Object in m_roomList)
			{
				if (roomid == item.roomid)
				{
					if (data.name) item.name = data.name;
					if (data.peilv) item.peilv = data.peilv;
					if (data.needgold) item.needgold = data.needgold;
					if (data.needlevel) item.needlevel = data.needlevel;
					if (data.numPeople) item.numPeople = data.numPeople;
					if (data.maxPeople) item.maxPeople = data.maxPeople;
					m_roomList[i] = item;
					break;
				}
				i++;
			}
		}
		
		/**
		 * 獲取當前房間信息
		 * @return	{id:"", name:"", peilv:"", needlevel:"", needgold:"", numPeople:"", maxPeople:"", gameid:"", gameName:"", gameNameCN:""}
		 */
		public function getCurrRoomInfo():Object
		{
			return getRoomInfoById(m_currRoomid);
		}
		
		public function get currRoomid():int 
		{
			return m_currRoomid;
		}
		
		public function set currRoomid(value:int):void 
		{
			m_currRoomid = value;
		}
		
		/**
		 * 獲取兌換數據
		 * @return
		 * 		{
		 * 			roomid:{defaultValue:"", minValue:"", maxValue:"", numPeople:""}
		 * 		}
		 */
		public function getDuihuanDatas():Object 
		{
			return m_duihuanDatas;
		}
		
		public function setDuihuanDatas(value:Object):void 
		{
			m_duihuanDatas = value;
		}
		
		
		
	}

}